package com.scholarship.modules.backend.controller;

import com.scholarship.common.entity.RetCode;
import com.scholarship.common.entity.RetResponse;
import com.scholarship.common.entity.RetResult;
import com.scholarship.modules.backend.dao.DateStartAndEnd;
import com.scholarship.modules.backend.entity.RewardType;
import com.scholarship.modules.backend.exce.TimeException;
import com.scholarship.modules.backend.service.RewardTimeService;
import com.scholarship.modules.login.entity.RewardTime;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @author zhz
 * @date 2021/8/30 10:23
 * 备注：奖学金时间的CURD
 */
@Api(tags = {"奖学金时间的CURD"})
@RestController
public class RewardTimeController {
    //日志logger
    private static final Logger logger = LoggerFactory.getLogger(RewardTimeController.class);

    @Autowired
    RewardTimeService rewardTimeService;

    @ApiOperation("增加奖学金申请时间 @author zhz")
    @PostMapping("/admin/addSubmitTime")
    public RetResult addSubmitTime(@RequestBody DateStartAndEnd date){
        try{
            rewardTimeService.addSubmitTime(date.getStart(),date.getEnd());
            return RetResponse.makeRsp(RetCode.SUCCESS,"成功增加奖学金申请时间");
        }catch (TimeException e){
            return RetResponse.makeRsp(RetCode.FAIL,e.getLocalizedMessage());
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return RetResponse.makeRsp(RetCode.INTERNAL_SERVER_ERROR,"出现未知错误");
        }
    }

    @ApiOperation("增加奖学金申诉时间 @author zhz")
    @PostMapping("/admin/addAppealTime")
    public RetResult addAppealTime(@RequestBody DateStartAndEnd date){
        try{
            rewardTimeService.addAppealTime(date.getStart(),date.getEnd());
            return RetResponse.makeRsp(RetCode.SUCCESS,"成功增加奖学金申诉时间");
        }catch (TimeException e){
            return RetResponse.makeRsp(RetCode.FAIL,e.getLocalizedMessage());
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return RetResponse.makeRsp(RetCode.INTERNAL_SERVER_ERROR,"出现未知错误");
        }
    }

    @ApiOperation("同时增加奖学金申请、申诉时间 @author zhz")
    @PostMapping("/admin/addRewardTime")
    public RetResult addRewardTime(@RequestBody RewardTime reward){
        try{
            rewardTimeService.addRewardTime(reward);
            return RetResponse.makeRsp(RetCode.SUCCESS,"成功同时增加奖学金申请、申诉时间");
        }catch (TimeException e){
            return RetResponse.makeRsp(RetCode.FAIL,e.getLocalizedMessage());
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return RetResponse.makeRsp(RetCode.INTERNAL_SERVER_ERROR,"出现未知错误");
        }
    }

    @ApiOperation("删除奖学金奖学金申请、申诉时间 @author zhz")
    @PostMapping("/admin/deleteRewardTime")
    public RetResult deleteRewardTime(){
        try{
            rewardTimeService.deleteRewardTime();
            return RetResponse.makeRsp(RetCode.SUCCESS,"成功删除奖学金奖学金申请、申诉时间");
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return RetResponse.makeRsp(RetCode.INTERNAL_SERVER_ERROR,"出现未知错误");
        }
    }

    @ApiOperation("删除奖学金奖学金申请时间 @author zhz")
    @PostMapping("/admin/deleteSubmitTime")
    public RetResult deleteSubmitTime(){
        try{
            rewardTimeService.deleteSubmitTime();
            return RetResponse.makeRsp(RetCode.SUCCESS,"成功删除奖学金奖学金申请时间");
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return RetResponse.makeRsp(RetCode.INTERNAL_SERVER_ERROR,"出现未知错误");
        }
    }

    @ApiOperation("删除奖学金奖学金申诉时间 @author zhz")
    @PostMapping("/admin/deleteAppealTime")
    public RetResult deleteAppealTime(){
        try{
            rewardTimeService.deleteAppealTime();
            return RetResponse.makeRsp(RetCode.SUCCESS,"成功删除奖学金奖学金申诉时间");
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return RetResponse.makeRsp(RetCode.INTERNAL_SERVER_ERROR,"出现未知错误");
        }
    }

    @ApiOperation("获取奖学金时间 @author zhz")
    @GetMapping({"/admin/getRewardTime","/student/getRewardTime"})
    public RetResult getRewardTime(){
        try{
            RewardTime reward = rewardTimeService.getRewardTime();
            return RetResponse.makeRsp(RetCode.SUCCESS,"成功获取奖学金时间",reward);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return RetResponse.makeRsp(RetCode.INTERNAL_SERVER_ERROR,"出现未知错误");
        }
    }
}
